package com.organization

import grails.test.*

class EmployeeIntegrationTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testAddEmployee() {
		def dep = new Department(name:"BU WAC").save()
		def proj = new Project(department: dep, name:"Tektronix",startDate: new Date()).save()
        def foundProj = Project.findByName("Tektronix")
        assertNotNull foundProj.id

        foundProj.addToEmployees(new Employee(lastName:"Ivanov",firstName: "Ivan"))
		foundProj.addToEmployees(new Employee(lastName:"Petrov",firstName: "Ivan"))
		foundProj.addToEmployees(new Employee(lastName:"Nesterov",firstName: "Ivan"))
        assertEquals 3, foundProj.employees.size()
    }
	
	void testAccessEmployees() {
		def dep = new Department(name:"BU WAC").save()
		dep.addToProjects(new Project(name:"Tektronix",startDate: new Date()))
        def proj = Project.findByName("Tektronix")
        assertNotNull proj.id

        proj.addToEmployees(new Employee(lastName:"Ivanov",firstName: "Ivan"))
		proj.addToEmployees(new Employee(lastName:"Petrov",firstName: "Ivan"))
		proj.addToEmployees(new Employee(lastName:"Nesterov",firstName: "Ivan"))
        def employeeNames = proj.employees.collect{it.lastName}
        assertEquals(["Ivanov","Nesterov","Petrov"],employeeNames.sort())
	}

    void testDeleteEmployee() {
        def dep = new Department(name:"BU WAC").save()
        def proj = new Project(name:"Tektronix",startDate: new Date())
        dep.addToProjects(proj)
        def foundProj = Project.findByName("Tektronix")
        assertNotNull foundProj.id

        foundProj.addToEmployees(new Employee(lastName:"Ivanov",firstName: "Ivan"))
        foundProj.addToEmployees(new Employee(lastName:"Petrov",firstName: "Ivan"))
        foundProj.addToEmployees(new Employee(lastName:"Nesterov",firstName: "Ivan"))

        def delEmployee = Employee.findByLastName("Nesterov")
        assertNotNull delEmployee.id

        delEmployee.delete()
        foundProj.removeFromEmployees(delEmployee)
        assertFalse Employee.exists(delEmployee.id)

        def foundDep = Department.get(dep.id)
        assertEquals 1, foundDep.projects.size()

        def foundProj1 = Project.findByName("Tektronix")
        assertNotNull foundProj1.id

        assertEquals 2, foundProj1.employees.size()
    }

    void testEvilSave(){
        def dep = new Department(name:"BU IP").save()
        def proj = new Project(name: "Tektronix",startDate: new Date())
        dep.addToProjects(proj)

        def empl = new Employee(
                lastName: "I",
                firstName: "P",
                extensionPhone: "123",
                homePhone: "12",
                mobilePhone: "+790512345",
                email: "abc@abc",
                addressStreetCity: "asghaslgkhdflgjdalfghalkgjhafsghlgalghaafg dlgjh adlkgjhlg",
                gender: "Male"
        )

        assertFalse empl.validate()
        assertTrue empl.hasErrors()
        assertNull empl.save()

        def errors = empl.errors
        assertEquals "size.toosmall",errors.getFieldError("lastName").code
        assertEquals "I",errors.getFieldError("lastName").rejectedValue
        assertEquals "size.toosmall",errors.getFieldError("firstName").code
        assertEquals "P",errors.getFieldError("firstName").rejectedValue
        assertEquals "matches.invalid",errors.getFieldError("extensionPhone").code
        assertEquals "123",errors.getFieldError("extensionPhone").rejectedValue
        assertEquals "matches.invalid",errors.getFieldError("homePhone").code
        assertEquals "12",errors.getFieldError("homePhone").rejectedValue
        assertEquals "matches.invalid",errors.getFieldError("mobilePhone").code
        assertEquals "+790512345",errors.getFieldError("mobilePhone").rejectedValue
        assertEquals "email.invalid",errors.getFieldError("email").code
        assertEquals "abc@abc",errors.getFieldError("email").rejectedValue
        assertEquals "size.toobig",errors.getFieldError("addressStreetCity").code
        assertEquals "asghaslgkhdflgjdalfghalkgjhafsghlgalghaafg dlgjh adlkgjhlg",
                errors.getFieldError("addressStreetCity").rejectedValue
        assertEquals "not.inList",errors.getFieldError("gender").code
        assertEquals "Male",errors.getFieldError("gender").rejectedValue

        empl.lastName = "Ivanov"
        empl.firstName = "Petr"
        empl.extensionPhone = "1234"
        empl.homePhone = "1234"
        empl.mobilePhone = "+79051919891"
        empl.email = "igorn@mail.ru"
        empl.addressStreetCity = "Rodionova, NN"
        empl.gender = "M"
        proj.addToEmployees(empl)
        assertTrue empl.validate()
        assertFalse empl.hasErrors()

        def foundDep = Department.findByName("BU IP")
        assertNotNull foundDep.id

        def foundProj = Project.findByName("Tektronix")
        assertNotNull foundProj.id

        def foundEmpl = Employee.findByLastName("Ivanov")
        assertNotNull foundEmpl.id

    }
}
